package com.inossem.warehousestorage_inossem.activity.inventory;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.bean.inventory.InventoryAreaBean;
import com.inossem.warehousestorage_inossem.bean.inventory.InventoryRespBean;
import com.inossem.warehousestorage_inossem.constant.RequestConstant;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.inventory.InventoryManageRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.InventoryUtils;
import com.inossem.warehousestorage_inossem.utils.SPUtils;

public class InventoryCommitActivity extends BaseActivity {

    private TextView title;
    private TextView confirm,inventoryVoucher,needUp,doUp,operator;
    private InventoryAreaBean.DataBean bean ;
    private String inventoryVoucherStr;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_inventory_commit);
        initView(InventoryCommitActivity.this);
        initData(this);
    }


    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        confirm = (TextView)activity.findViewById(R.id.confirm);
        inventoryVoucher = (TextView)activity.findViewById(R.id.inventoryVoucher);
        needUp = (TextView)activity.findViewById(R.id.needUp);
        doUp = (TextView)activity.findViewById(R.id.doUp);
        operator = (TextView)activity.findViewById(R.id.operator);

    }

    private void initData(Activity activity) {
        title.setText(activity.getResources().getString(R.string.activity_up_frame_commit_title));
        ActivityUtils.initBackAndFunction(activity, activity.getResources().getString(R.string.activity_inventory_overview_title), new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });

        bean = SPUtils.getInformation(activity,InventoryAreaBean.DataBean.class,SPUtils.KEY_INVENTORY_INFORMATION);
        inventoryVoucherStr = activity.getIntent().getStringExtra(InventoryExtra.INVENTORY_VOUCHER);

        inventoryVoucher.setText(inventoryVoucherStr);
        needUp.setText(InventoryUtils.getTotalRowCount(bean)+"");
        doUp.setText(InventoryUtils.getTotalCheckCount(bean)+"");
        operator.setText(SPUtils.getName(this));

        confirm.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {

                InventoryManageRequest.commit(activity,inventoryVoucherStr,new GsonBeanListener<InventoryRespBean>(){
                    @Override
                    public void onSuccess(InventoryRespBean bean) {
                        if(bean.getData().getCode().equals(RequestConstant.BEAN_CODE_SUCCESS)){
                            Intent intent = new Intent(activity, InventoryResultActivity.class);
                            startActivity(intent);
                        }else{
                            Utils.middleToast(activity, "提交失败:"+bean.getData().getMsg());
                        }

                    }
                });

            }
        });



    }



    @Override
    public void onBackPressed() {
            finish();
    }
}
